<template>
  <div class="roles-container">
    <div class="app-container">
      <el-card>
        <el-row type="flex" justify="end">
          <el-button type="primary" size="small" @click="addFn">添加角色</el-button>
        </el-row>

        <el-table :data="formData" border style="width: 100%">
          <el-table-column prop="title" label="角色名称" />
          <el-table-column prop="description" label="角色描述" />
          <el-table-column prop="address" label="操作">
            <template v-slot="{row}">
              <el-button size="small" type="primary" @click="assignPerm(row.id)">分配权限</el-button>
              <el-button size="small" @click="editFn(row.id)">编辑</el-button>
              <el-button size="small" type="danger" @click="delFn(row.id)">删除</el-button>
            </template>
          </el-table-column>
          <!-- </el-col> -->
        </el-table>
        <el-pagination
          background
          :page-size="limit"
          layout="prev, pager, next"
          :total="total"
          @current-change="changePage"
        />
      </el-card>
      <AddRoles ref="addRole" :show-dialog.sync="visible" @updateEvent="getRolesList" />
      <!-- 分配权限 -->
      <AssignPerm
        ref="assignPerm"
        :show-dialog.sync="showDialog"
      />
    </div>
  </div>
</template>

<script>
import AssignPerm from './components/assign-perm.vue'
import AddRoles from './components/add-roles.vue'

import { getRoles, delRoles, rolesNum } from '@/api/roles'
export default {
  components: {
    AddRoles,
    AssignPerm

  },
  data() {
    return {
      formData: [
        { title: '',
          description: '' }
      ],
      limit: 5, // 当前页的数量
      total: 0,
      visible: false,
      showDialog: false,
      start: 0
    }
  },
  async  created() {
    this.getRolesList()
    const res = await rolesNum()
    console.log(res, 60)
    this.total = res.data
  },
  // 获取角色列表
  methods: {
    async  getRolesList() {
      const res = await getRoles({
        _limit: this.limit,
        _start: this.start
      })
      console.log(res)
      if (this.limit === 0 && this.total > 0) {
        this.start = 0
      }
      this.formData = res.data
    },
    // 新增角色
    addFn() {
      this.visible = true
    },
    // 编辑
    editFn(id) {
      this.visible = true
      this.$refs.addRole.getRolesListById(id)
    },
    async  delFn(id) {
      this.$confirm('是否删除角色？', '删除', {
        type: 'warning'
      }).then(async() => {
        // 调用接口
        await delRoles(id)
        // 刷新权限
        this.getRolesList()
      }).cath(() => {
        console.log('点击了取消按钮')
      })
    },
    // 分配权限
    assignPerm(id) {
      this.showDialog = true
      this.$refs.assignPerm.editRolesById(id)
    },
    changePage(page) {
      // 如果点击的页面等于1第一页，
      // 如果不等于1那就是用当前页面减一，在乘以页面的条数
      this.start = page === 1 ? 0 : (page - 1) * this.limit
      console.log(page)
      this.getRolesList()
    }

  }

}
</script>

<style>
.el-table {
  margin-top: 20px;
}
</style>
